/**
 * 工作流服务
 *
 * @author majintao
 * @version 1.0.0, 2017/11/24
 * @see workflowCtrl
 * @since 工作流
 */

(function (app, ng) {
    'use strict';

    /**
     * 注册服务
     */
    app.service('worlflowService', Service);

    /**
     * 依赖项
     */
    Service.$inject = ['dialogs','$http'];

    // 常量声明
    var CATEGORYS={1:"这是分类一",2:"这是分类二"};
                  

    /**
     * 服务
     * @constructor
     */
    function Service(dialogs,$http) {
        // 初始化
        var s = this;
        //extendResource('patrol/plan'); // 继承restful服务
        
        //方法引用
        this.createModel = createModel;
        this.tableOptions = tableOptions;
        this.deployModel = deployModel;
        this.deleteModel = deleteModel;
        
        function createModel(param){
        	return $http.post(
    				ISS.getPath('/act/create'),
    				param,
    	            {contentType: 'json'}
    				).then(function(resp){
    					return resp.data;
    				});
        }
        
        /**
         * 获取表格配置
         */
        function tableOptions() {
            var _gridWidth = 1024;
            var _columnWidth = _gridWidth / 7;
            return {
                url: ISS.getPath('/act/list'),
                dataType: 'json',
                //extParam: params,
                usepager: true,
                showcheckbox: false,
                pageMode: 'jpa',
                rp: 10,
                isDragCol: true,
                colModel: [
                    {display: 'workflow.model.category', name: 'category', width: _columnWidth ,process:function(value){
                    	return CATEGORYS[value];
                    }},
                    {display: 'workflow.model.modelId', name: 'id', width: _columnWidth * 2.22},
                    {display: 'workflow.model.key', name: 'key', width: _columnWidth},
                    {display: 'workflow.model.name', name: 'name', width: _columnWidth * 1.18},
                    {display: 'workflow.model.revision', name: 'revision', width: _columnWidth*0.6},
                    {display: 'workflow.model.createTime', name: 'createTime', width: _columnWidth*1.18,process:function(value){
                    	return formatTime(value);
                    }},
                    {display: 'workflow.model.lastUpdateTime', name: 'lastUpdateTime', width: _columnWidth*1.18,process:function(value){
                    	return formatTime(value);
                    }},
                    {
                        display: 'common.operation', name: "id", width: 200, process: function (value, index, module) {
                        var html = [];
                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.editModel(\'' + value + '\')">');
                        html.push('<span translate="workflow.model.operation.edit"></span>');
                        html.push('</button>');
                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.deployModel(\'' + value + '\')">');
                        html.push('<span translate="workflow.model.operation.deploy"></span>');
                        html.push('</button>');
                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.exportModel(\'' + value + '\')">');
                        html.push('<span translate="workflow.model.operation.export"></span>');
                        html.push('</button>');
                        html.push('<button class="btn m-r-xs btn-xs btn-danger" ng-click="vm.deleteModel(\'' + value + '\')">');
                        html.push('<span translate="workflow.model.operation.delete"></span>');
                        html.push('</button>');
                        return html.join('');
                    }
                    }
                ]
            };
        }
        /**
         * 部署模型
         */
        function deployModel(modelId)
        {
        	return $http.get(
    				ISS.getPath('/act/deploy/'+modelId)
    				).then(function(resp){
    					return resp.data;
    				});
        }
        
        /**
         * 删除模型
         */
        function deleteModel(modelId)
        {
        	return $http.get(
    				ISS.getPath('/act/delete/'+modelId)
    				).then(function(resp){
    					return resp.data;
    				});
        }
        /**
         * 何世华时间显示格式，默认YYYY-MM-DD hh:mm:ss
         */
        function formatTime(date,fmt)
        {
        	fmt || (fmt = "YYYY-MM-DD hh:mm:ss");
        	return moment(date).format(fmt);
        }
    }
})(iApp.module(), window.angular);